package com.netty.exmple.pooled;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;

/**
 * 非内存池模式
 * 测试非内存池模式，消耗的时间和GC的次数和时间
 *
 * @author hjj
 * @create 2019-12-04 11:09
 */
public class UnpooledTest {
    public static void main(String[] args) {
        // 执行时间84028，YGC：1202次，FGC：0次
//        unpooledTest();
        // 执行时间15829，YGC：0次，FGC：0
        pooledTest();
    }

    public static void unpooledTest() {
        long beginTime = System.currentTimeMillis();
        ByteBuf buf = null;
        int maxTimes = 100000000;
        for (int i = 0; i < maxTimes; i++) {
            buf = Unpooled.buffer(10 * 1024);
            buf.release();
        }
        System.out.println("Execute " + maxTimes + " times cost time: " + (System.currentTimeMillis() - beginTime));

    }

    public static void pooledTest() {
        PooledByteBufAllocator allocator = new PooledByteBufAllocator(false);
        long beginTime = System.currentTimeMillis();
        ByteBuf buf = null;
        int maxTimes = 100000000;
        for (int i = 0; i < maxTimes; i++) {
            buf = allocator.heapBuffer(10 * 1024);
            buf.release();
        }
        System.out.println("Execute " + maxTimes + " times cost time: " + (System.currentTimeMillis() - beginTime));
    }

    public static void test() {

    }
}
